package com.greedy.bulls.stock.user.service.impl;

import com.greedy.bulls.stock.common.exception.ComponentException;
import com.greedy.bulls.stock.common.utils.GlobalConstants;
import com.greedy.bulls.stock.common.exception.constants.ApplicationErrorCodeEnum;
import com.greedy.bulls.stock.common.generator.GlobalIdGenerator;
import com.greedy.bulls.stock.entity.user.TradeUserFile;
import com.greedy.bulls.stock.user.dao.ITradeUserFileDao;
import com.greedy.bulls.stock.user.minio.MinioOperator;
import com.greedy.bulls.stock.user.service.IStockUserFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-08-05
 * Description: 用户文件信息表服务实现类
 */
@Service
public class StockUserFileServiceImpl implements IStockUserFileService {

    @Autowired
    private MinioOperator minioOperator;

    @Autowired
    private GlobalIdGenerator globalIdGenerator;

    @Autowired
    private ITradeUserFileDao tradeUserFileDao;

    @Value("${minio.bucketName}")
    private String bucketName;

    @Override
    public String uploadUserIdCard(Long userId, MultipartFile file) throws Exception {
        // 对文件判空校验
        if (null == file) {
            throw new ComponentException(ApplicationErrorCodeEnum.USER_FILE_NOT_FOUND);
        }
        // 生成唯一ID
        String remoteFileId = globalIdGenerator.nextStrId();
        // 上传文件
        minioOperator.uploadFile(remoteFileId, new MultipartFile[]{file}, bucketName);
        // 查找对应用户文件
        TradeUserFile dbTradeUserFile = tradeUserFileDao.getByUserIdAndBizType(userId, GlobalConstants.FILE_BIZ_TYPE_IDCARD);
        if (null == dbTradeUserFile) {
            // 新增用户文件
            dbTradeUserFile = new TradeUserFile();
            dbTradeUserFile.setBizType(GlobalConstants.FILE_BIZ_TYPE_IDCARD);
            dbTradeUserFile.setFilename(file.getOriginalFilename());
            dbTradeUserFile.setFileType(file.getContentType());
            dbTradeUserFile.setFileId(remoteFileId);
            dbTradeUserFile.setUserId(userId);
            dbTradeUserFile.setCreateTime(new Date());
            tradeUserFileDao.insert(dbTradeUserFile);
        } else {
            // 清空原有对象
            minioOperator.delFile(dbTradeUserFile.getFileId(), bucketName);
            // 更新用户文件
            dbTradeUserFile.setFileId(remoteFileId);
            dbTradeUserFile.setFilename(file.getOriginalFilename());
            dbTradeUserFile.setFileType(file.getContentType());
            tradeUserFileDao.update(dbTradeUserFile);
        }
        return remoteFileId;
    }

    /**
     * 根据文件ID查找文件对象
     * @param fileId 文件ID
     * @return 文件对象
     */
    @Override
    public TradeUserFile getTradeUserFile(String fileId) throws ComponentException {
        TradeUserFile byFileId = tradeUserFileDao.getByFileId(Long.valueOf(fileId));
        if (null == byFileId) {
            throw new ComponentException(ApplicationErrorCodeEnum.USER_FILE_NOT_FOUND);
        }
        return byFileId;
    }

}
